package cn.com.jrj.service;

import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;


import org.apache.commons.httpclient.HttpException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.jrj.base.util.ConfigUtil;
import cn.com.jrj.base.util.HttpSnatch;
import cn.com.jrj.domain.StockInfo;

/**
 * 天津联通下发短信service
 * 
 * @author xiaoyu.dou
 * @see http://qxt.jrj.com.cn/qxtOA/help/HTTPService.html 企信通接口说明
 */
public class SmsService {
	/** smsuserID */
	private static final String USERID = ConfigUtil.getInstance().getString("userID");

	/** smsurl */
	private static final String URL = ConfigUtil.getInstance().getString("url");

	/** 编码方式 */
	private static final String ENCODE = "utf-8";

	/** 超时时间 */
	private static final int TIMEOUT = 5000;
	
	/**log*/
	private Log log=LogFactory.getLog(getClass());
	private static HttpSnatch http = HttpSnatch.getInstance();

	/**
	 * 向指定手机号下发短信
	 * 
	 * @param phoneNum
	 * @param content
	 * @throws IOException
	 * @throws HttpException
	 * @throws ParserConfigurationException
	 */
	public boolean sendMessage(String phoneNum, String content) throws HttpException, IOException, ParserConfigurationException {
		String param = "&s=" + USERID + "&m=" + content +"&p="+phoneNum;
		String s = http.GetWebContent(URL + param, ENCODE, TIMEOUT);

		return parseResult(s);
	}

	/**
	 * 向指定的手机下发短信内容
	 * 
	 * @param stockInfo
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 * @throws ParserConfigurationException
	 */
	public boolean sendMessage(StockInfo stockInfo) throws HttpException, IOException, ParserConfigurationException {
		String content = this.generateContent(stockInfo);
		String param = "&s=" + USERID + "&m=" + content +"&p="+stockInfo.getMobile();
		String s = http.GetWebContent(URL + param, ENCODE, TIMEOUT);
		return parseResult(s);
	}

	/**
	 * 批量下发短信,返回已经下发的自选股的ID串(用逗号分隔)
	 * @param stockInfoList
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 * @throws ParserConfigurationException
	 */
	public String sendMessage(List<StockInfo> stockInfoList) throws HttpException, IOException, ParserConfigurationException {
		String result = "";
		for (StockInfo sf : stockInfoList) {
			if (sendMessage(sf)) {
				result += sf.getMystock_id() + ",";
				log.info("ZZZ: send sms succeed: " + sf.getUid() + "," + sf.getMystock_id() + "," + sf.getMobile() + "," + sf.getStock_name());
			} else {
				log.error("ZZZ: send sms failed!!!: " + sf.getUid() + "," + sf.getMystock_id() + "," + sf.getMobile() + "," + sf.getStock_name());
			}
		}
		if (result.endsWith(",")) {
			result = result.substring(0, result.length() - 1);
		}
		return result;
	}

	/**
	 * 组织短信的内容
	 * 
	 * @param stockInfo
	 * @return
	 */
	public String generateContent(StockInfo stockInfo) {
		StringBuffer sb = new StringBuffer();
		sb.append("我是股神：股票 ");
		sb.append(stockInfo.getStock_name() + "(" + stockInfo.getStock_code() + "，现价"+ stockInfo.getNow_price() + " 涨幅" + stockInfo.getZdf() +"%)，已");
		sb.append(stockInfo.getWarn_condition() == StockInfo.WARN_CONDITION_GREAT ? "大于" : "小于");
		sb.append("设定价位 " + stockInfo.getWarn_price() + "元。");
		sb.append("请您及时关注！");
		return sb.toString();
	}

	/**
	 * 专门解析企信通http接口返回的数据<BR/>
	 * 
	 * @param xmlResult
	 * @return
	 */
	private boolean parseResult(String s) {
		return "1".equals(s);
	}
}
